package service;

import static constant.Constant.STR_USER_ADDED;
import static constant.Constant.STR_USER_ALREADY_EXIST;
import static constant.Constant.STR_USER_DELETED;
import static constant.Constant.STR_USER_EDITED;
import static constant.Constant.STR_USER_NOT_EXIST;

import java.util.HashMap;
import java.util.Map.Entry;

public class UserManager {

	private static UserManager userManagerInstance;

	public static UserManager getInstance() {
		if (userManagerInstance == null) {
			userManagerInstance = new UserManager();
		}
		return userManagerInstance;
	}

	private HashMap<String, String> userList;

	private UserManager() {
		userList = new HashMap<String, String>();
		add("allo", "Adrien");
		add("toto", "Michel");
	}

	public String add(String surnom, String nom) {
		if (userList.containsKey(surnom)) {
			return STR_USER_ALREADY_EXIST;
		} else {
			userList.put(surnom, nom);
		}
		return STR_USER_ADDED;
	}

	public String toString() {
		String string = new String();
		for (Entry<String, String> user : userList.entrySet()) {
			string += user.getKey() + " " + user.getValue() + "\n";
		}
		return string;
	}

	public String delete(String surnom) {
		if (!userList.containsKey(surnom)) {
			return STR_USER_NOT_EXIST;
		} else {
			userList.remove(surnom);
		}
		return STR_USER_DELETED;
	}

	public String show(String surnom) {
		if (!userList.containsKey(surnom)) {
			return STR_USER_NOT_EXIST;
		}
		return userList.get(surnom);
	}

	public String edit(String surnom, String nom) {
		if (!userList.containsKey(surnom)) {
			return STR_USER_NOT_EXIST;
		} else {
			userList.put(surnom, nom);
		}
		return STR_USER_EDITED;
	}
}
